package com.czxy.servlet;

import cn.itcast.servlet.BaseServlet;
import com.czxy.damain.Student;
import com.czxy.service.StudentService;

import javax.servlet.annotation.WebServlet;
import java.util.List;

@WebServlet("/studentServlet")
public class StudentServlet extends BaseServlet {
    private StudentService studentService = new StudentService();


    public  String   findStudentById(){
        String sid = getRequest().getParameter("sid");


        String student = studentService.findStudentById(sid);

        getRequest().setAttribute("student",student);

        return "/student/view.jsp";


    }









    //编辑

    public  String update(){

        Student student = toBean(Student.class);


        boolean rel = studentService.update(student);


        if (rel){

            return "redirect:/studentServlet?method=findAll";

        }else{

            getRequest().setAttribute("student",student);
            getRequest().setAttribute("updateMsg","编辑失败,登录名重复");
            return "/student/edit.jsp";
        }









    }









    public  String   edit(){

        String sid = getRequest().getParameter("sid");

        Student student = studentService.findStudentId(sid);


        getRequest().setAttribute("student",student);

        return "/student/edit.jsp";


    }

























    public  String add(){

        Student student = toBean(Student.class);

        boolean rel = studentService.add(student);


        if (rel){

            //成功
            return "redirect:/studentServlet?method=findAll";
        }else {

            getRequest().setAttribute("student",student);
            getRequest().setAttribute("addMsg","添加失败,登录名已存在");

            return "/student/add.jsp";
        }


    }















    public  String delete(){


        String sid = getRequest().getParameter("sid");

        studentService.delete(sid);


        return "redirect:/studentServlet?method=findAll";
    }




















    //根据姓名查询


    public  String   findName (){

        String name = getRequest().getParameter("studentname");

        List<Student> list = studentService.findName(name);

        getRequest().setAttribute("studentList",list);



        return "/student/list.jsp";

    }














    //查询所有

    public String  findAll(){

        List<Student> list = studentService.findAll();


        getRequest().setAttribute("studentList",list);


        return "/student/list.jsp";

    }




}
